package com.bkhech.home.practice.lottery;

import java.util.Random;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.statistics.HistogramDataset;
import javax.swing.JFrame;

/**
 * RandomDistribution
 *
 * 生成 100 万次 0-100 之间的随机数
 *
 * @author guowm
 * @date 2024/6/28
 */
public class RandomDistribution {
    public static void main(String[] args) throws InterruptedException {
         paint();
    }

    private static void paint() throws InterruptedException {
        // 生成 100 万次 0-100 之间的随机数。1000000
        double[] randomNumbers = new double[1000000];
        Random random = new Random();
        for (int i = 0; i < randomNumbers.length; i++) {
            // 生成 0 到 100 之间的随机整数
            randomNumbers[i] = random.nextInt(101);
        }

        // 创建数据集
        HistogramDataset dataset = new HistogramDataset();
        dataset.addSeries("Random Numbers", randomNumbers, 101, 0, 100);

        // 创建直方图
        JFreeChart histogram = ChartFactory.createHistogram(
                "Distribution of 1,000,000 Random Numbers between 0 and 100",
                "Number",
                "Frequency",
                dataset,
                PlotOrientation.VERTICAL,
                false,
                true,
                false
        );

        // 显示图表
        ChartPanel chartPanel = new ChartPanel(histogram);
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(chartPanel);
        frame.pack();
        frame.setVisible(true);
    }
}
